#include <Arduino.h>

#include "scheduler_basic.hpp"
#include "scheduler_tick.hpp"

using TimeSource = scheduler_basic::ArduinoMsSource;
using TimeType = uint32_t;

scheduler_basic::DelayCallback3<TimeSource, 10> dcb;

// ======================= MAIN ======================

TimeType test() {
    Serial.print("HELLO HELLO\n");
    Serial.print("Task Count = ");
    Serial.print(dcb.task_count());
    Serial.print("\n");
    return 800;
}


class Executor : public scheduler_basic::TaskObject<Executor, TimeType> {
    public:
    int counter = 0;

    TimeType task() {
        Serial.print("OBJECT! ");
        Serial.print(counter);
        Serial.print("\n");
        ++counter;
        return 500;
    }

    ~Executor() {
        // 任务对象（TaskObjectBase）析构时，会自动将其自身移出任务列表
        Serial.print("Object Destructed\n");
    }
};

void setup() {
    Serial.begin(115200);

    Executor *e = new Executor{};

    dcb.add_task(test, 0);
    dcb.add_task(e, 0);

    while (1) {
        dcb.tick();

        if (e != nullptr && e->counter > 10) {
            delete e;
            e = nullptr;
        }
    }
}


void loop() { /* DO NOTHING */ }